package com.logansoft.servicehomework.controller;

import com.logansoft.servicehomework.entity.CommonResult;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liqiye
 * @description 测试分布式锁
 * @date 2021/7/16
 */

@RestController
public class TestRedissionController {

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/testLock")
    public CommonResult testLock() {

        //1 锁的名字，锁的粒度，越细越快
        RLock lock = redissonClient.getLock("catalogJson-lock");
        System.out.println(lock);
        //加锁
        lock.lock();
        try {
            //业务代码
        }finally {
            lock.unlock();
        }
        return new CommonResult(200,"成功");
    }

}
